package com.opengroupe.dto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.opengroupe.database.qRHSQLiteHelper;

public class LightResultatQuestionnaire implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3412664721774737860L;
	
	private CandidatDto candidat;
	
	private List<SingleQuestionReponse> listeSingleQuestionReponse;
	
	
	
	public LightResultatQuestionnaire() {
	}
	
	public LightResultatQuestionnaire(CandidatDto candidatDto,
			List<SingleQuestionReponse> listeSingleQuestionReponse) {
		super();
		this.candidat = candidatDto;
		this.listeSingleQuestionReponse = listeSingleQuestionReponse;
		
	}

	public CandidatDto getCandidat() {
		return candidat;
	}
	
	public void setCandidat(CandidatDto candidatDto) {
		this.candidat = candidatDto;
	}
	
	public List<SingleQuestionReponse> getQuestions() {
		return listeSingleQuestionReponse;
	}
	
	public List<SingleQuestionReponse> setQuestions(List<SingleQuestionReponse> questions) {
		return listeSingleQuestionReponse = questions;
	}
	
	
	
	public double getPourcentage() {
		
		int sommeReponse = 0;
		
		for(int i=0; i<listeSingleQuestionReponse.size();i++ ){			
			
			sommeReponse += listeSingleQuestionReponse.get(i).questionCorrecte() ? 1 : 0;
					
		}
		
		return sommeReponse / listeSingleQuestionReponse.size() * 100;
	}
	
	public boolean isFilled()
	{
		
		for(int i=0; i< listeSingleQuestionReponse.size(); i++)
		{
			if (listeSingleQuestionReponse.get(i).getIdsReponsesCandidat().size()>0)
			{
				return true;
			}
		}
		
		return false;
	}
	
	

	
	
	public static LightResultatQuestionnaire getFromBdd(Context context, String emailCandidat) {
		
		LightResultatQuestionnaire res = new LightResultatQuestionnaire();
		
		qRHSQLiteHelper bddHelper = new qRHSQLiteHelper(context);
		SQLiteDatabase bdd = bddHelper.getReadableDatabase();
	    
	    Cursor cursor = null;
	    
	    //Candidat
		
		cursor = bdd.query(
				"candidat",
				new String[] { "id","nom","prenom","email","telephone","idQuestionnaire" },
				"email=?", 
				new String[] { emailCandidat},
				null,null,null,null);
		
		cursor.moveToFirst();
		
		if  (!cursor.isAfterLast()) {
			res.setCandidat( new CandidatDto(
	    			cursor.getInt(0),
	    			cursor.getString(1),
	    			cursor.getString(2),
	    			cursor.getString(3),
	    			cursor.getString(4),
	    			cursor.getInt(5)
	    			));
	    }
	    
	    cursor.close();
	    
	    
	    
	    // Questionnaire
	    QuestionnaireDto questionnaire = QuestionnaireDto.getFromBdd(context, res.getCandidat().getIdQuestionnaire());
	    
	    List<QuestionDto> questions = questionnaire.getQuestions();
	    
	    
	    //Reponses
	
		cursor = bdd.query(
				"candidatReponseQuestion",
				new  String[] { "idReponse","idQuestion" },
				"idCandidat=?", 
				new String[] { String.valueOf(res.getCandidat().getIdCandidat())},
				null,null,null,null);
		
		cursor.moveToFirst();
				
		HashMap<Integer, SingleQuestionReponse> listeQuestion = new HashMap<Integer, SingleQuestionReponse>();
		
		int idQuestion = 0;
		int idReponse = 0;
		ArrayList<Integer> idsReponseCandidat = null;
		
		while  (!cursor.isAfterLast()) {
			
			idReponse = cursor.getInt(0);
			idQuestion = cursor.getInt(1);
			
			if (listeQuestion.containsKey(idQuestion))
			{
				listeQuestion.get(idQuestion).getIdsReponsesCandidat().add(idReponse);
			}
			else
			{
				int index = questions.indexOf(new QuestionDto(idQuestion, "", 0));
				
				if (index>=0)
				{
					idsReponseCandidat = new ArrayList<Integer>();
					idsReponseCandidat.add(idReponse);
					
					listeQuestion.put(
							idQuestion, 
							new SingleQuestionReponse( questions.get(index), idsReponseCandidat	)
					);
				}
			}
			
			cursor.moveToNext();
	    }
	    
	    cursor.close();
	    
	    for(int i=0; i< questions.size(); i++)
	    {
	    	 
	    	if (!listeQuestion.containsKey(questions.get(i).getIdQuestion()))
	    	{
	    		listeQuestion.put(
	    				questions.get(i).getIdQuestion(), 
	    				new SingleQuestionReponse(questions.get(i), new ArrayList<Integer>())
	    				);
	    	}
	    }
	    
	    res.setQuestions(new ArrayList<SingleQuestionReponse>( listeQuestion.values()));
	    
	    bdd.close();
	    bddHelper.close();
	    
		return res;
	}
}
